package gemini.castor.ui.client.page.widget.personalinfo;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.date.DateWidget;

import java.util.Iterator;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.validation.client.InvalidConstraint;
import com.google.gwt.validation.client.interfaces.IValidator;

public class PersonalInfoWidget extends Composite {

	@UiTemplate("PersonalInfoWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, PersonalInfoWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	private IValidator<PersonalInfoObject> validator;
	private PersonalInfoObject object;
	private I18nConstants constants;

	public void setPersonalInfoObject(PersonalInfoObject object) {
		this.object = object;
	}

	public PersonalInfoWidget() {
		initWidget(binder.createAndBindUi(this));
		validator = GWT.create(PersonalInfoObject.class);
		constants = GWT.create(I18nConstants.class);
		initData();
	}

	private void initData() {
		String sex[] = (constants.stringSex()).split(",");

		for (String item : sex) {
			String[] sexValue = item.split(":");
			lbxSex.addItem(sexValue[0],sexValue[1]);
		}
	}

	@UiField
	TextBox tbxFirstName;
	
	@UiField
	TextBox tbxLastName;

	@UiField
	ListBox lbxSex;

	@UiField
	DateWidget dwgBirthDay;

	public boolean isValidate() {
		resetStyle();
		bind();
		boolean result = true;
		Set<InvalidConstraint<PersonalInfoObject>> invalidConstraints = validator
				.validate(object);
		Iterator<InvalidConstraint<PersonalInfoObject>> it = invalidConstraints
				.iterator();

		while (it.hasNext()) {
			if (result) {
				result = false;
			}
			InvalidConstraint<PersonalInfoObject> ic = it.next();
			showError(ic.getItemName(), ic.getMessage());
		}

		return result;
	}

	private void resetStyle() {
		tbxFirstName.removeStyleDependentName("error");
		tbxLastName.removeStyleDependentName("error");
	}

	private void showError(String widgetName, String errorString) {
		if (widgetName.equals("firstName")) {
			tbxFirstName.addStyleDependentName("error");
			
			
		} else if (widgetName.equals("lastName")) {
			tbxLastName.addStyleDependentName("error");
		}
	}

	public void bind() {
		object.setFirstName(tbxFirstName.getValue());
		object.setLastName(tbxLastName.getValue());
		object.setBirthDay(dwgBirthDay.getDateFromDateWidget());
		object.setSex(lbxSex.getValue(lbxSex.getSelectedIndex()));
		
	}

	public void reset() {
		resetStyle();
		resetValue();
	}
	
	public void resetValue() {
		tbxFirstName.setValue("", true);
		tbxLastName.setValue("", true);
		lbxSex.setItemSelected(0, true);
		dwgBirthDay.reset();
	}

	public void setObject(PersonalInfoObject object) {
		this.object = object;
	}
}
